package com.zhou.base.components.feign.simple.controller;

import cn.hutool.core.util.StrUtil;
import com.zhou.base.components.bs.common.exception.base.BaseExceptionBuilder;
import com.zhou.base.components.bs.common.model.Response;
import com.zhou.base.components.feign.simple.util.TestSignatureUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import static com.zhou.base.components.feign.simple.constant.TestConstant.APP_KEY_NAME;
import static com.zhou.base.components.feign.simple.constant.TestConstant.APP_SIGN_NAME;
import static com.zhou.base.components.feign.simple.constant.TestConstant.KEY_AND_SCRET_MAP;
import static com.zhou.base.components.feign.simple.constant.TestConstant.TOKEN_KEY_NAME;
import static com.zhou.base.components.feign.simple.constant.TestConstant.TOKEN_KEY_VALUE;
import static com.zhou.base.components.feign.simple.util.TestSignatureUtil.APP_KEY;
import static com.zhou.base.components.feign.simple.util.TestSignatureUtil.APP_SECRET;

/**
 * 测试类 feign的异常捕获
 *
 * @author xianfeng
 * @date 2023/11/22 16:21
 * @description:
 */
@RequiredArgsConstructor
@RefreshScope
@RestController
@RequestMapping("/test")
public class TestController {


    private final HttpServletRequest request;

    @GetMapping("")
    public Response getNormal(String param) {
        return Response.success(param);
    }

    @GetMapping("/getToken")
    public Response getToken(String appKey, String appSecret) {
        if (APP_KEY.equals(appKey) && APP_SECRET.equals(appSecret)) {
            return Response.success(TOKEN_KEY_VALUE);
        }
        throw BaseExceptionBuilder.message("秘钥错误");
    }

    @GetMapping("/queryByAuth")
    public Response queryByAuth(String param) {
        //1.验证签名
        String appKey = request.getHeader(APP_KEY_NAME);
        String outsideSign = request.getHeader(APP_SIGN_NAME);
        String secret = KEY_AND_SCRET_MAP.get(appKey);
        if (StrUtil.isEmpty(secret)) {
            throw BaseExceptionBuilder.message("签名错误");
        }
        String sign = TestSignatureUtil.getSign(param, secret);
        if (!sign.equals(outsideSign)) {
            throw BaseExceptionBuilder.message("签名错误");
        }
        //2.验证token
        String authorization = request.getHeader(TOKEN_KEY_NAME);
        if (!TOKEN_KEY_VALUE.equals(authorization)) {
            throw BaseExceptionBuilder.message("token错误");
        }
        return Response.success(param);
    }

    @ApiOperation("测试系统内部的错误")
    @GetMapping("/query/error")
    public Response queryWithError(String param) {
        if (1 == 1) {
            throw BaseExceptionBuilder.message("方法执行错误了");
        }
        return Response.success(param);
    }

    @ApiOperation("测试错误的状态码")
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @GetMapping("/query/errorStatus")
    public Response queryWithErrorStatus(String param) {
        return Response.fail(param);
    }

    @ApiOperation("测试错误的状态码")
    @PostMapping(value = "/upload", headers = "Content-Type=multipart/form-data")
    public Response upload(@RequestParam("file") MultipartFile file) {
        return Response.success(file.getOriginalFilename());
    }
}
